//	ID : pallab81
// 	PROG : 10819
// 	LANG : C++
// 	2011-07-17-00.31.27 Sunday
//
// 	"I have not failed, I have just found 10000 ways that won't work.
//


#include <iostream>
#include <algorithm>
#include <vector>
#include <sstream>
#include <fstream>
#include <string>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <functional>
#include <bitset>

#include <cassert>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <climits>
#include <cstring>

using namespace std;

#define fo(i,ed) for(int i = 0 ; i < ed ; ++i )
#define foit(i, x) for (typeof((x).begin()) i = (x).begin(); i != (x).end(); i++)
#define bip system("pause")
#define Int long long
#define pb push_back
#define SZ(X) (int)(X).size()
#define LN(X) (int)(X).length()
#define mk make_pair
#define f first
#define s second
#define SET( ARRAY , VALUE ) memset( ARRAY , VALUE , sizeof(ARRAY) )

const int inf = (int)20000;
const int inf_n = (int)150;
int m,n;
int p[inf_n],f[inf_n];
int dp[ inf+100 ];

inline void read() {
    int i;
    for(i=1;i<=n;++i) {
        scanf("%d %d",&p[i],&f[i]);
    }
}

inline void proc() {
    SET(dp,0);
    int j;
    int i;

    for(i=1;i<=n;++i){
        for(j=inf;j>=p[i];--j){
            if( j==p[i] ){
                dp[j] = max(dp[j], f[i]+dp[j-p[i]]);
            }
            else if( (j>p[i]) && dp[j-p[i]]!=0 ){
                dp[j] = max(dp[j], f[i]+dp[j-p[i]]);
            }
        }
    }
    int best = 0;
    for(i=1;i<=m+200;++i){
        if(i<=m){
            best = max(best, dp[i]);
        }
        else{
            if(i>2000){
                best = max(best, dp[i]);
            }
        }
    }
    printf("%d\n",best);
}

int main() {
#ifndef ONLINE_JUDGE
    freopen("in.txt","rt",stdin);
#endif
    while( scanf("%d %d",&m,&n)==2 ) {
        read();
        proc();
    }


    return 0;
}


